package io.finer.erp.jeecg.finance.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.api.client.util.Lists;

import cn.hutool.core.date.CalendarUtil;
import cn.hutool.core.date.DateUtil;
import io.finer.erp.jeecg.bas.entity.SalesDeliveryEntry;
import io.finer.erp.jeecg.bas.service.IBasCustomerService;
import io.finer.erp.jeecg.bas.service.ISalesDeliveryEntryService;
import io.finer.erp.jeecg.bas.vo.PrintProductVO;
import io.finer.erp.jeecg.bas.vo.SalesDeliveryOrEntryVO;
import io.finer.erp.jeecg.finance.dto.FinaReceiptAddDto;
import io.finer.erp.jeecg.finance.dto.FinaReceiptDto;
import io.finer.erp.jeecg.finance.dto.FinaReceiptPrintDTO;
import io.finer.erp.jeecg.finance.dto.ZrkShoporderDto;
import io.finer.erp.jeecg.finance.entity.FinaReceipt;
import io.finer.erp.jeecg.finance.service.IFinaReceiptService;
import io.finer.erp.jeecg.finance.vo.DeliverDetalidVO;
import io.finer.erp.jeecg.finance.vo.DeliverySumVO;
import io.finer.erp.jeecg.finance.vo.DeliverySummaryVO;
import io.finer.erp.jeecg.finance.vo.FinaReceiptPage;
import io.finer.erp.jeecg.finance.vo.FinaReceiptPageSumVO;
import io.finer.erp.jeecg.finance.vo.FinaReceiptSKVo;
import io.finer.erp.jeecg.finance.vo.FinaReceiptSumPage;
import io.finer.erp.jeecg.finance.vo.FinaReceiptSumVO;
import io.finer.erp.jeecg.finance.vo.FinaReceiptVO;
import io.finer.erp.jeecg.finance.vo.ReceiptSumVO;
import io.finer.erp.jeecg.finance.vo.ReceiptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 收款表
 * @Author: jeecg-boot
 * @Date: 2021-03-22
 * @Version: V1.0
 */
@Api(tags = "收款表")
@RestController
@RequestMapping("/finance/finaReceipt")
@Slf4j
public class FinaReceiptController extends JeecgController<FinaReceipt, IFinaReceiptService> {
	@Autowired
	private IFinaReceiptService finaReceiptService;
	@Autowired
	private IBasCustomerService customerService;
	@Autowired
	private ISalesDeliveryEntryService entryService;

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "收款表-应收已收客户账单分页列表查询")
	@ApiOperation(value = "收款表-应收已收客户账单分页列表查询", notes = "收款表-应收已收客户账单分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FinaReceipt finaReceipt,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		QueryWrapper<FinaReceipt> queryWrapper = QueryGenerator.initQueryWrapper(finaReceipt, req.getParameterMap());
		Page<FinaReceipt> page = new Page<FinaReceipt>(pageNo, pageSize);
		IPage<FinaReceipt> pageList = finaReceiptService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "收款单-应收/已收")
	@ApiOperation(value = "收款单-应收/已收", notes = "收款单金额统计-应收/已收")
	@GetMapping(value = "/sumList")
	public Result<?> queryPageList(FinaReceiptVO finaReceipt,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Page<FinaReceiptPage> page = new Page<FinaReceiptPage>(pageNo, pageSize);
		List<FinaReceiptPage> list = finaReceiptService.getPageReceipt(finaReceipt);
		return Result.ok(page.setRecords(list));
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "收款单-应收/已收列表金额合计")
	@ApiOperation(value = "收款单-应收/已收列表金额合计", notes = "收款单金额统计-应收/已收列表金额合计")
	@GetMapping(value = "/listZDSum")
	public Result<?> queryPageListSum(FinaReceiptPage finaReceipt) {

		BigDecimal receivablesEnd = finaReceipt.getReceivablesEnd();
		BigDecimal receivablesStart = finaReceipt.getReceivablesStart();
		BigDecimal receivedsEnd = finaReceipt.getReceivedsEnd();
		BigDecimal receivedsStart = finaReceipt.getReceivedsStart();
		BigDecimal invoicePriceStart = finaReceipt.getInvoicePriceStart();
		BigDecimal invoicePriceEnd = finaReceipt.getInvoicePriceEnd();
		BigDecimal discountSumPriceStart = finaReceipt.getDiscountSumPriceStart();
		BigDecimal discountSumPriceEnd = finaReceipt.getDiscountSumPriceEnd();

		FinaReceiptPageSumVO pageList = finaReceiptService.getPageReceiptSum(finaReceipt.getCreateTimeStart(),
				finaReceipt.getCreateTimeEnd(), finaReceipt.getCustomerName(), receivablesEnd, receivablesStart,
				receivedsEnd, receivedsStart, invoicePriceStart, invoicePriceEnd, discountSumPriceStart,
				discountSumPriceEnd);
		return Result.ok(pageList);

	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "根据客户id查询发货单信息")
	@ApiOperation(value = "根据客户id查询发货单信息", notes = "根据客户id查询发货单信息")
	@GetMapping(value = "/deliverList")
	public Result<?> queryPageDeleiverList(SalesDeliveryOrEntryVO entryVO,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
//		 IPage<SalesDeliveryOrEntryVO> page = new Page<SalesDeliveryOrEntryVO>(pageNo, pageSize);
//		 IPage<FinaReceiptPage> pageList = finaReceiptService.getPageReceipt(page,finaReceipt.getCreateTimeStart(),finaReceipt.getCreateTimeEnd(),finaReceipt.getCustomerName());
		Map<String, Object> map = finaReceiptService.getDeliveryOrEntry(entryVO.getType(),entryVO.getCustomerId()
				 ,entryVO.getCreateTimeStart(),entryVO.getCreateTimeEnd(),
				 entryVO.getSalesType(),entryVO.getCode(),entryVO.getProductName(),
				 entryVO.getViceQtyStart(),entryVO.getViceQtyEnd(),entryVO.getQtyStart(),
				 entryVO.getQtyEnd(),entryVO.getUnitPriceStart(),entryVO.getUnitPriceEnd(),
				 entryVO.getTotalPriceStart(),entryVO.getTotalPriceEnd(),entryVO.getTotalAmountStart(),
				 entryVO.getTotalAmountEnd(),entryVO.getHxPriceStart(),entryVO.getHxPriceEnd(),
				 entryVO.getZkPriceStart(),entryVO.getZkPriceEnd(),entryVO.getQkPriceStart(),
				 entryVO.getQkPriceEnd(),entryVO.getRemark());
		return Result.ok(map);
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "收款单-当天已收客户账单分页列表查询")
	@ApiOperation(value = "收款单-当天已收客户账单分页列表查询", notes = "收款单-当天已收客户账单分页列表查询")
	@GetMapping(value = "/sklist")
	public Result<?> queryPageSkList(FinaReceipt finaReceipt,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
//		 QueryWrapper<FinaReceipt> queryWrapper = QueryGenerator.initQueryWrapper(finaReceipt, req.getParameterMap());
//		 Page<FinaReceipt> page = new Page<FinaReceipt>(pageNo, pageSize);
		Page<FinaReceiptSKVo> page1 = new Page<FinaReceiptSKVo>(pageNo, pageSize);

		/*************************** 默认当月 *****************************************/
		// 获取本月第一天
//		 Calendar calendar = Calendar.getInstance();
//		 calendar.setTime(new Date());
//		 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
//		 Date s = calendar.getTime();
//		 //获取本月最后一天
//		 Calendar calendar2 = Calendar.getInstance();
//		 calendar2.setTime(new Date());
//		 calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
//		 Date e = calendar2.getTime();
//		 queryWrapper.between("create_time", DateUtils.formatDate(s, "yyyy/MM/dd"),
//				 DateUtils.formatDate(e, "yyyy/MM/dd"));
		/*****************************
		 * 默认当天
		 **********************************************/
//		 queryWrapper.eq("create_time", DateUtils.formatDate(new Date(), "yyyy/MM/dd"));
//
//
//		 queryWrapper.eq("bill_type","2");
//		 queryWrapper.groupBy("customer_id");
//		 IPage<FinaReceipt> pageList = finaReceiptService.page(page, queryWrapper);
		String createTimeStartStr = req.getParameter("createTimeStart");
		String createTimeEndStr = req.getParameter("createTimeEnd");
		Date createTimeStart = null;
		Date createTimeEnd = null;
		if (StringUtils.isNotBlank(createTimeStartStr)) {
			createTimeStart = DateUtil.parseDate(createTimeStartStr);
		}
		if (StringUtils.isNotBlank(createTimeEndStr)) {
			createTimeEnd = DateUtil.parseDate(createTimeEndStr);
			Calendar calendar = CalendarUtil.calendar(createTimeEnd);
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			createTimeEnd = calendar.getTime();
		}
		String cashType = req.getParameter("cashType");
		IPage<FinaReceiptSKVo> pageList = finaReceiptService.getPageReceiptSK(page1, createTimeStart, createTimeEnd,
				finaReceipt.getCustomerName(), cashType, req);
		return Result.ok(pageList);
	}

	/**
	 * @Description 导出Excel
	 * @param finaReceipt
	 * @return Create at: 2021-05-24 17:37:11
	 * @author: duany Revision: 2021-05-24 17:37:11 - first revision by duany
	 * 
	 */
	@RequestMapping(value = "/exportExcel")
	public ModelAndView exportExcel(FinaReceipt finaReceipt, HttpServletRequest req) {
		String title = "收款单";
		// return null;
		// Step.1 组装查询条件=2021-05-17&createTimeEnd=2021-05-25
		String createTimeStartStr = req.getParameter("createTimeStart");
		String createTimeEndStr = req.getParameter("createTimeEnd");
		Date createTimeStart = null;
		Date createTimeEnd = null;
		String customerName = req.getParameter("customerName");
		if (StringUtils.isNotBlank(createTimeStartStr)) {
			createTimeStart = DateUtil.parseDate(createTimeStartStr);
		}
		if (StringUtils.isNotBlank(createTimeEndStr)) {
			createTimeEnd = DateUtil.parseDate(createTimeEndStr);
			Calendar calendar = CalendarUtil.calendar(createTimeEnd);
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			createTimeEnd = calendar.getTime();
		}
		List<FinaReceiptSKVo> list = this.finaReceiptService.excelGet(createTimeStart, createTimeEnd, customerName);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, FinaReceiptSKVo.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}

	@AutoLog(value = "收款表-根据编号查询单据详情")
	@ApiOperation(value = "收款表-根据编号查询单据详情", notes = "收款表-根据编号查询单据详情")
	@GetMapping(value = "/detailsList")
	public Result<?> detailsList(FinaReceipt finaReceipt) {
		List<SalesDeliveryEntry> list = entryService.getSalesDeliveryEntryList(finaReceipt.getOrderNo());
//		 List<PrintProductVO> list = zrkDeliverEntryService.getBillEntryByBillNo(finaReceipt.getOrderNo(), finaReceipt.getBillType());
		return Result.ok(list);
	}

	/**
	 * 添加已收
	 *
	 * @param finaReceipt
	 * @return
	 */
	@AutoLog(value = "收款表-添加已收")
	@ApiOperation(value = "收款表-添加已收", notes = "收款表-添加已收")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FinaReceiptAddDto finaReceipt) {
		return this.finaReceiptService.addReceipt(finaReceipt);
	}
	@AutoLog(value = "收款表-添加收款")
	@ApiOperation(value = "收款表-添加收款", notes = "收款表-添加收款")
	@PostMapping(value = "/addReceipt")
	public Result<?> addReceipt(@RequestBody ReceiptVO vo){
		return this.finaReceiptService.addReceipt(vo);
	}
	
	@AutoLog(value = "收款表-修改收款")
	@ApiOperation(value = "收款表-修改收款", notes = "收款表-修改收款")
	@PostMapping(value = "/updateReceipt")
	public Result<?> updateReceipt(@RequestBody ReceiptVO vo){
		return this.finaReceiptService.updateReceipt(vo);
	}
	

	/**
	 * 编辑
	 *
	 * @param finaReceipt
	 * @return
	 */
	@AutoLog(value = "收款表-编辑")
	@ApiOperation(value = "收款表-编辑", notes = "收款表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FinaReceipt finaReceipt) {
		finaReceiptService.updateById(finaReceipt);
		finaReceiptService.marchFlat(finaReceipt);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "收款表-通过id删除")
	@ApiOperation(value = "收款表-通过id删除", notes = "收款表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		finaReceiptService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "收款表-批量删除")
	@ApiOperation(value = "收款表-批量删除", notes = "收款表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.finaReceiptService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "收款表-通过id查询")
	@ApiOperation(value = "收款表-通过id查询", notes = "收款表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		FinaReceipt finaReceipt = finaReceiptService.getById(id);
		if (finaReceipt == null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(finaReceipt);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param finaReceipt
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, FinaReceipt finaReceipt) {
		return export(request, finaReceipt, "收款详情");
	}

	protected ModelAndView export(HttpServletRequest request, FinaReceipt object, String title) {
		// 获取本月第一天
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		Date s = calendar.getTime();
		// 获取本月最后一天
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(new Date());
		calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date e = calendar2.getTime();

		String customerId = object.getCustomerId();
		object.setCustomerId("");
		// Step.1 组装查询条件
		QueryWrapper<FinaReceipt> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
		queryWrapper.between("create_time", DateUtils.formatDate(s, "yyyy/MM/dd"),
				DateUtils.formatDate(e, "yyyy/MM/dd"));
		queryWrapper.eq("customer_id", customerId);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<FinaReceipt> pageList = finaReceiptService.list(queryWrapper);
		List<SalesDeliveryEntry> exportDelivery = Lists.newArrayList();
		for (FinaReceipt receipt : pageList) {
			if (StringUtils.isNotBlank(receipt.getOrderNo())) {
				exportDelivery = entryService.getSalesDeliveryEntryList(receipt.getOrderNo());
			}
		}

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, SalesDeliveryEntry.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, exportDelivery);
		return mv;
	}
	
	/**
	 * 发货汇总导出excel
	 *
	 * @param request
	 * @param finaReceipt
	 */
	@RequestMapping(value = "/exportXlsDeliver")
	public ModelAndView exportXlsDeliver(DeliverySummaryVO detalidVO) {
		return exportDeliver(detalidVO, "发货汇总");
	}

	// 导出当月统计账单数据
	protected ModelAndView exportDeliver(DeliverySummaryVO object, String title) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<DeliverySummaryVO> pageList = finaReceiptService.getDeliver(object);

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, DeliverySummaryVO.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}
	
	/**
	 * 发货明细汇总导出excel
	 *
	 * @param request
	 * @param finaReceipt
	 */
	@RequestMapping(value = "/exportXlsEntry")
	public ModelAndView exportXlsEntry(DeliverDetalidVO detalidVO) {
		return exportEntry(detalidVO, "发货明细汇总");
	}

	// 导出当月统计账单数据
	protected ModelAndView exportEntry(DeliverDetalidVO object, String title) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<DeliverDetalidVO> pageList = finaReceiptService.getDeliverDetali(object);

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, DeliverDetalidVO.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param finaReceipt
	 */
	@RequestMapping(value = "/exportXlsSum")
	public ModelAndView exportXlsSum(HttpServletRequest request, FinaReceiptDto finaReceipt) {
		return exportSum(request, finaReceipt, "统计信息");
	}

	// 导出当月统计账单数据
	protected ModelAndView exportSum(HttpServletRequest request, FinaReceiptDto object, String title) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
//		 List<FinaReceipt> pageList = finaReceiptService.list(queryWrapper);
		List<FinaReceiptDto> pageList = finaReceiptService.getReceiptExcel(object.getCreateTimeStart(),
				object.getCreateTimeEnd(), object.getCustomerName());
		List<List<PrintProductVO>> lists = Lists.newArrayList();
//		 for (FinaReceiptDto receipt : pageList) {
//			 List<PrintProductVO> list= zrkDeliverEntryService.getBillEntryByBillNo(receipt.getBillNo(),"1");
//			 lists.add(list);
//		 }

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, FinaReceiptDto.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, FinaReceipt.class);
	}

	/***************************************
	 * 收款单汇总
	 *************************************************************/

	@AutoLog(value = "收款单-统计金额总数")
	@ApiOperation(value = "收款单-统计金额总数", notes = "收款单-统计金额总数")
	@GetMapping(value = "/amountSk")
	public Result<?> amount(FinaReceiptSumVO finaReceiptSumVO, HttpServletRequest request) {
		String createTimeStartStr = finaReceiptSumVO.getCreateTimeStart();
		String createTimeEndStr = finaReceiptSumVO.getCreateTimeEnd();
		Date createTimeStart = null;
		Date createTimeEnd = null;
		if (StringUtils.isNotBlank(createTimeStartStr)) {
			createTimeStart = DateUtil.parseDate(createTimeStartStr);
		}
		if (StringUtils.isNotBlank(createTimeEndStr)) {
			createTimeEnd = DateUtil.parseDate(createTimeEndStr);
			Calendar calendar = CalendarUtil.calendar(createTimeEnd);
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			createTimeEnd = calendar.getTime();
		}
		FinaReceiptSumVO getamount = finaReceiptService.getamountSK(createTimeStart,
				createTimeEnd, finaReceiptSumVO.getCustomerName(), finaReceiptSumVO.getCashType()
				, request);
		return Result.ok(getamount);
	}

	@AutoLog(value = "收款汇总单-统计汇总金额总数")
	@ApiOperation(value = "收款汇总单-统计汇总金额总数", notes = "收款汇总单-统计汇总金额总数")
	@GetMapping(value = "/amountSum")
	public Result<?> amountSum(ReceiptSumVO finaReceiptSumVO) {
		ReceiptSumVO getamount = finaReceiptService.getamountSum(finaReceiptSumVO.getCreateTimeStart(),
				finaReceiptSumVO.getCreateTimeEnd(), finaReceiptSumVO.getCustomerName());
		return Result.ok(getamount);
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "收款单-已收汇总统计")
	@ApiOperation(value = "收款单-已收汇总统计", notes = "收款单金额统计-已收汇总统计")
	@GetMapping(value = "/summary")
	public Result<?> queryPageLists(FinaReceiptSumPage finaReceipt,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Page<FinaReceiptSumPage> page = new Page<FinaReceiptSumPage>(pageNo, pageSize);
		IPage<FinaReceiptSumPage> pageList = finaReceiptService.getPageReceiptSumPage(page,
				finaReceipt.getCreateTimeStart(), finaReceipt.getCreateTimeEnd(), finaReceipt.getCustomerName());
		return Result.ok(pageList);
	}

	/********************************************
	 * 获取销售单列表
	 ************************************************************************/

	@AutoLog(value = "收款单-获取未核销完成的销售单列表")
	@ApiOperation(value = "收款单-获取未核销完成的销售单列表", notes = "收款单-获取未核销完成的销售单列表")
	@GetMapping(value = "/orderSk")
	public Result<?> getOrder(ZrkShoporderDto finaReceipt,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Result result = new Result();
		Page<ZrkShoporderDto> page = new Page<ZrkShoporderDto>(pageNo, pageSize);// 字典翻译。分页无效
		List<ZrkShoporderDto> dtos = finaReceiptService.getOrder(finaReceipt);
		page.setRecords(dtos);
		result.setResult(page);
		result.setCode(200);
		return result;
	}

	@AutoLog(value = "收款单-获取已核销的销售单列表")
	@ApiOperation(value = "收款单-获取已核销的销售单列表", notes = "收款单-获取已核销的销售单列表")
	@GetMapping(value = "/orderHXY")
	public Result<?> getOrderY(ZrkShoporderDto finaReceipt,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Result result = new Result();
		Page<ZrkShoporderDto> page = new Page<ZrkShoporderDto>(pageNo, pageSize);// 字典翻译。分页无效
		List<ZrkShoporderDto> dtos = finaReceiptService.getOrderY(finaReceipt);
		page.setRecords(dtos);
		result.setResult(page);
		result.setCode(200);
		return result;
	}

	/******************************************************************************************************************/

	/***************************************
	 * 发货
	 *******************************************************/

	@AutoLog(value = "收款单-发货汇总")
	@ApiOperation(value = "收款单-发货汇总", notes = "收款单-发货汇总")
	@GetMapping(value = "/getDeliver")
	public Result<?> getDeliver(DeliverySummaryVO summaryVO) {
		List<DeliverySummaryVO> dtos = finaReceiptService.getDeliver(summaryVO);
		return Result.ok(dtos);
	}

	@AutoLog(value = "收款单-发货汇总数量金额统计")
	@ApiOperation(value = "收款单-发货汇总数量金额统计", notes = "收款单-发货汇总数量金额统计")
	@GetMapping(value = "/deliverSum")
	public Result<?> deliverSum(DeliverySumVO sumVO) {
		DeliverySumVO dtos = finaReceiptService.getDeliverSum(sumVO);
		return Result.ok(dtos);
	}

	@AutoLog(value = "收款单-发货明细汇总")
	@ApiOperation(value = "收款单-发货明细汇总", notes = "收款单-发货明细汇总")
	@GetMapping(value = "/getDetail")
	public Result<?> getDeliverDetali(DeliverDetalidVO detalidVO) {
		List<DeliverDetalidVO> dtos = finaReceiptService.getDeliverDetali(detalidVO);
		if(dtos==null||dtos.size()==0) {
			dtos = Lists.newArrayList();
		}
		return Result.ok(dtos);
	}
	
	/**
	 * 发货单明细表
	 *
	 * @param SalesDeliveryOrEntryVO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "发货单明细表")
	@ApiOperation(value = "发货单明细表", notes = "发货单明细表")
	@GetMapping(value = "/getSalesList")
	public Result<?> getSalesDeliveryAndEntry(SalesDeliveryOrEntryVO vo,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Page<SalesDeliveryOrEntryVO> page = new Page<SalesDeliveryOrEntryVO>(pageNo, pageSize);
		IPage<SalesDeliveryOrEntryVO> pageList = finaReceiptService.getSalesDeliveryAndEntry(page, vo);
		return Result.ok(pageList);
	}
	
	@ApiOperation(value="获取客户出货明细", notes="获取客户出货明细")
	@GetMapping(value = "/getCustomerDelivery")
	public Result<?> getCustomerDelivery(@RequestParam("customerId")String customerId){
		return this.finaReceiptService.getCustomerDelivery(customerId);
	}
	
	@ApiOperation(value="获取收款单详情", notes="获取收款单详情")
	@GetMapping(value = "/getReceiptInfo")
	public Result<ReceiptVO> getReceiptInfo(String receiptId){
		return this.finaReceiptService.getReceiptInfo(receiptId);
	}
	
	/**
	 * 导出（打印）对账单余额
	 *
	 * @param SalesDeliveryOrEntryVO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "导出（打印）对账单余额")
	@ApiOperation(value = "导出（打印）对账单余额", notes = "导出（打印）对账单余额")
	@GetMapping(value = "/printFinaReceipt")
	public Result<?> printFinaReceipt(FinaReceiptPage vo) {
		if(oConvertUtils.isEmpty(vo.getCreateTimeStart())) {
			return Result.error("请选择起始日期");
		}
		if(oConvertUtils.isEmpty(vo.getCreateTimeEnd())) {
			return Result.error("请选择截止日期");
		}
		if(oConvertUtils.isEmpty(vo.getCustomerId())) {
			return Result.error("未指定客户");
		}
		FinaReceiptPrintDTO dto = finaReceiptService.getSalesDeliveryByTime(vo);
		return Result.ok(dto);
	}
	
	/**
	 * @Description 记录客户扎帐日期
	 * @param vo
	 * @return
	 * Create at: 2021-06-09 16:09:14
	 * @author: xzs
	 * Revision:
	 *    2021-06-09 16:09:14 - first revision by xzs
	 *
	 */
	@AutoLog(value = "记录客户扎帐日期")
	@ApiOperation(value = "记录客户扎帐日期", notes = "记录客户扎帐日期")
	@PostMapping(value = "/printCustomerChargeTime")
	public Result<?> printCustomerChargeTime(@RequestBody FinaReceiptPage vo) {
		if(oConvertUtils.isEmpty(vo.getCreateTimeStart())) {
			return Result.error("请选择扎帐起始日期");
		}
		if(oConvertUtils.isEmpty(vo.getCreateTimeEnd())) {
			return Result.error("请选择扎帐截止日期");
		}
		if(oConvertUtils.isEmpty(vo.getCustomerId())) {
			return Result.error("未指定客户");
		}
		boolean result = finaReceiptService.printChargeTime(vo);
		if(result) {
			return Result.ok("记录成功");
		}
		return Result.error("记录失败");
	}
	
	/**
	 * @Description 应收账款余额表导出
	 * @param finaReceipt
	 * @param req
	 * @return
	 * Create at: 2021-09-19 15:33:27
	 * @author: xzs
	 * Revision:
	 *    2021-09-19 15:33:27 - first revision by xzs
	 *
	 */
	@RequestMapping(value = "/exportExcelReceivable")
	public ModelAndView exportExcelReceivable(FinaReceiptVO finaReceipt, HttpServletRequest req) {
		String title = "应收账款余额";
		List<FinaReceiptPage> list = this.finaReceiptService.getPageReceipt(finaReceipt);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, FinaReceiptPage.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}
}
